#ifndef SCREEN_H
#define SCREEN_H

#include <QImage>
#include <QPainter>
#include <QWidget>

class Screen : public QWidget
{
    Q_OBJECT
public:
    explicit Screen(QWidget *parent = nullptr)
        : QWidget(parent), m_image(256, 240, QImage::Format_ARGB32)
    {
        m_image.fill(Qt::white);
    }

public slots:
    void update(const quint8 frame_data[256][240][3])
    {
        for (int x = 0; x < 256; x++) {
            for (int y = 0; y < 240; y++) {
                QColor color(frame_data[x][y][0], frame_data[x][y][1], frame_data[x][y][2]);
                m_image.setPixelColor(x, y, color);
            }
        }
    }

protected:
    void paintEvent(QPaintEvent *event)
    {
        Q_UNUSED(event);

        QPainter painter(this);

        // create target rect
        auto size = this->size();
        QRectF target(0, 0, size.rwidth(), size.rheight());

        // source rect
        QRectF source(0, 0, 256, 240);

        // draw screen
        painter.drawImage(target, m_image, source);
    }

private:
    QImage m_image;
};

#endif // SCREEN_H
